package service

import (
	"Gin/app/dao"
	"Gin/app/vo"
	"Gin/middle/jwt"
	"Gin/utils/Redis"
)

type RootService struct {
}

var RDao dao.RootDao

func (rs *RootService) Login(from *vo.LoginVo) *vo.RootVo {
	root := RDao.FindRootByName(from.UserName)
	rPass := getMd5Pass(from.Password)
	rPass = getMd5Pass(rPass)
	if root == nil || rPass != root.Password {
		return nil
	}
	token, _ := jwt.GenerateToken(root.Id, root.CreateTime, root.Root)
	client := Redis.GetClient()
	client.Set(client.Context(), Redis.RootKey+token, root, Redis.KeyExpireTime)
	rootVo := &vo.RootVo{
		Id:         root.Id,
		Root:       root.Root,
		Times:      root.Times,
		CreateTime: root.CreateTime,
		UpdateTime: root.UpdateTime,
		Token:      token,
	}
	return rootVo
}
